<? if (!defined("BASEPATH")) exit("No direct script access allowed");

class EventsModel extends CI_Model
{

    public function getEvents() {
        $this->db->select("*");
        $this->db->from("events");
        $this->db->where("`date` >= now()");
        $result_set = $this->db->get();
        return ($result_set->result());
    }

    public function getParticipants($id) {
        $this->db->select("*,event_participants.weight as weight");
        $this->db->from("event_participants");
        $this->db->where("event_id", $id);
        $this->db->join("users", "event_participants.user_id = users.id", "left");
        $result_set = $this->db->get();

        return ($result_set->result());
    }

    public function getParticipant($event_id, $user_id) {
        $this->db->select("weight");
        $this->db->from("event_participants");
        $this->db->where("event_id", $event_id);
        $this->db->where("user_id", $user_id);
        $result_set = $this->db->get();
        $result = $result_set->result();
        return ($result[0]);
    }

    public function createEvent() {
        $data = array(
            "title" => $this->input->post("event_title"),
            "description" => $this->input->post("description"),
            "damage" => $this->input->post("damage"),
            "date" => $this->input->post("event_date"),
            "event_time" => $this->input->post("event_time"),
            "place" => $this->input->post("place"),
            "creator_id" => $this->tank_auth->get_user_id(),
            "allow_intro" => $this->input->post("allow_intros"),
        );

        $this->db->insert("events", $data);
        $event_id = $this->db->insert_id();

        $this->db->flush_cache();
        $user_id = $this->tank_auth->get_user_id();


        $this->load->model('PostsModel');
        $this->PostsModel->insertPost("event", $event_id, $user_id);
    }

    public function addParticipant($event_id) {
        $data = array(
            "event_id" => $event_id,
            "user_id" => $this->tank_auth->get_user_id()
        );

        $this->db->insert("event_participants", $data);
    }

    public function upWeight($event_id) {
        $participant = $this->getParticipant($event_id, $this->tank_auth->get_user_id());
        $weight = $participant->weight;
        if ($weight < 9) {
            $data = array(
                "event_id" => $event_id,
                "weight" => $weight + 1
            );

            $this->db->where("event_id", $event_id);
            $this->db->where("user_id", $this->tank_auth->get_user_id());
            $this->db->update("event_participants", $data);
        }
    }

    public function removeParticipant($event_id) {
        $this->db->where("event_id", $event_id);
        $this->db->where("user_id", $this->tank_auth->get_user_id());
        $this->db->delete("event_participants");
    }

    public function countMessages($event_id) {
        $this->db->where("parent_event_id", $event_id);
        $this->db->from("posts");
        return $this->db->count_all_results();
    }
}